﻿using Anke.SHManage.BLL;
using Anke.SHManage.MSSQLDAL;
using Anke.SHManage.MSSQLDAL.TJDAL;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Anke.SHManage.Web.Reports
{
    public partial class LS_DeliveryModeWebForm : System.Web.UI.Page
    {
        private string reportName;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.storehouse.Visible = false;
            this.TargetStorage.Visible = false;
            if (UserOperateContext.Current.Session_UsrInfo == null) //add2023-08-07 plq
            {
                Response.Redirect("~/Home/RedirectToLogin");
            }
            else if (!IsPostBack)
            {
                InitPage();

            }
            else
            {
                reportName = Convert.ToString(ViewState["reportName"]);
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindReportDataSource();
            this.Material.Text = "";
        }
        private void InitPage()
        {
            this.StartDate.Text = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            this.EndDate.Text = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
            reportName = Request.QueryString["ReportName"];
            ViewState["reportName"] = reportName;
            BindReportDataSource();
        }

        protected void BindReportDataSource()
        {

            DateTime start = Convert.ToDateTime(this.StartDate.Text);
            DateTime end = Convert.ToDateTime(this.EndDate.Text);
            string sourceStorage = (this.SourceStorage.Text).ToString();
            string targetStorage = Convert.ToString(this.TargetStorage.Text);
            string classification = Convert.ToString(this.Classification.Text);
            string material = (this.Material.Text).ToString();
            string deliveryListCode = Convert.ToString(this.DeliveryListCode.Text);
            string deliveryType = Convert.ToString(this.DeliveryType.Text);
            string receiver = Convert.ToString(this.Receiver.Text);
            string receiverAmbulance = Convert.ToString(this.ReceiverAmbulance.Text);
            string operater= Convert.ToString(this.Operator.Text); //出库流水表新增经办人查询条件 2016-07-01 youhao
            //取登录人的仓库权限（仓库id）
            List<int> StroageIDs = new List<int>();
            StroageIDs = UserOperateContext.Current.Session_StorageRelated.listUserStorage;
            string instr = "";
            foreach (int item in StroageIDs)
            {
                instr += "'" + item + " ',";
            }
            instr = instr.Substring(0, instr.Length - 1);

            string start1 = start.ToString();
            string end1 = end.ToString();
            string targetStorage1 = targetStorage.ToString();

            this.ReportViewer1.LocalReport.DataSources.Clear();
           // this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(@"~/Reports/LS_DeliveryList.rdlc");
            this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(@"~/Reports/" + reportName);
            
            Microsoft.Reporting.WebForms.ReportParameter st = new Microsoft.Reporting.WebForms.ReportParameter("StartTime", start1);
            Microsoft.Reporting.WebForms.ReportParameter ed = new Microsoft.Reporting.WebForms.ReportParameter("EndTime", end1);
            //Microsoft.Reporting.WebForms.ReportParameter ts = new Microsoft.Reporting.WebForms.ReportParameter("TargetStorageID", targetStorage1);
            this.ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { st, ed });
            LSDAL dal = new LSDAL();
            TJDAL dat = new TJDAL();
            switch (reportName)
            {
                case "LS_DeliveryList.rdlc":
                    this.Page.Title = "出库流水表";
                    DataTable dt1 = dal.Get_LS_DeliveryList(start, end, sourceStorage, targetStorage, instr, classification, material,
                                                            deliveryListCode, deliveryType, receiver, receiverAmbulance, operater);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForLS_DeliveryList", dt1));
                    break;
                case "TJ_DeliveryList.rdlc":
                    this.Page.Title = "出库汇总表";
                    DataTable dt2 = dat.Get_DeliveryListHZ(start, end, sourceStorage, targetStorage, instr, classification,
                                                           material, deliveryListCode, deliveryType, receiver, receiverAmbulance);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForTJ_DeliveryList", dt2));
                    break;
                case "TJ_SLSYQK.rdlc":
                    this.Page.Title = "医生贵重药品申领使用情况表";
                    DataTable dt3 = dat.Get_SLSYQK(start, end, sourceStorage, targetStorage, instr, classification,
                                                           material, deliveryListCode, deliveryType, receiver, receiverAmbulance);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForTJ_SLSYQK", dt3));
                    break;
                case "LS_XDeliveryList.rdlc":
                    this.Page.Title = "新出库流水表";
                    //2018-12-11 zch 这些无用参数传到rdlc 没用
                    //Microsoft.Reporting.WebForms.ReportParameter dl = new Microsoft.Reporting.WebForms.ReportParameter("DeliveryListCode", deliveryListCode);
                    //Microsoft.Reporting.WebForms.ReportParameter d1 = new Microsoft.Reporting.WebForms.ReportParameter("Receiver", receiver);
                    //Microsoft.Reporting.WebForms.ReportParameter d2 = new Microsoft.Reporting.WebForms.ReportParameter("Operator", operater);
                    //this.ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { st, ed, dl,d1,d2 });
                    DataTable dt4 = dal.Get_LS_XDeliveryList(start, end, sourceStorage, targetStorage, instr, classification, material,
                                                            deliveryListCode, deliveryType, receiver, receiverAmbulance, operater);
                    this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetForLS_XDeliveryList", dt4));
                    break;
                default:
                    break;
            }
            this.ReportViewer1.LocalReport.Refresh();
        }
    }
}